package com.eien.phonezwei.activity;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.widget.Toast;

import com.eien.phonezwei.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    private SharedPreferences ePrefs;
    private EditTextPreference numPref;
    // private SwitchPreference contactOnlyPref;
    private SwitchPreference missedCallPref;
    private SwitchPreference forwardSmsPref;

    @Override
    @SuppressWarnings("deprecation")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.setting_preference);
        ePrefs = PreferenceManager.getDefaultSharedPreferences(this);
        numPref = (EditTextPreference) findPreference("sendToNumber");
        // contactOnlyPref = (SwitchPreference) findPreference("contactOnly");
        missedCallPref = (SwitchPreference) findPreference("forwardMissedCall");
        forwardSmsPref = (SwitchPreference) findPreference("forwardSMS");
        checkNumber();
        ePrefs.registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        switch (key) {
            case "sendToNumber":
                // System.out.println(prefs.getString(key, ""));
                checkNumber();
                break;
            case "forwardMissedCall":
                if (prefs.getBoolean("forwardMissedCall", false)) {
                    if (checkStringInt(prefs.getString("sendToNumber", "")) && !prefs.getString("sendToNumber", "").equals("")) {
                        ShowToast("未接来电通知服务已启动");
                    } else {
                        ShowToastLong("手机号未设置或者格式有误，请先设置手机号");
                        SharedPreferences.Editor mEditor = prefs.edit();
                        mEditor.putBoolean("forwardMissedCall", false);
                        mEditor.putBoolean("forwardSMS", false);
                        mEditor.apply();
                        missedCallPref.setChecked(false);
                        forwardSmsPref.setChecked(false);
                    }
                } else {
                    ShowToast("未接来电通知服务已关闭");
                }
                break;
            case "forwardSMS":
                if (prefs.getBoolean("forwardSMS", false)) {
                    if (checkStringInt(prefs.getString("sendToNumber", "")) && !prefs.getString("sendToNumber", "").equals("")) {
                        ShowToast("短信转发服务已启动");
                    } else {
                        ShowToastLong("手机号未设置或者格式有误，请先设置手机号");
                        SharedPreferences.Editor mEditor = prefs.edit();
                        mEditor.putBoolean("forwardMissedCall", false);
                        mEditor.putBoolean("forwardSMS", false);
                        mEditor.apply();
                        missedCallPref.setChecked(false);
                        forwardSmsPref.setChecked(false);
                    }
                } else {
                    ShowToast("短信转发服务已关闭");
                }
                break;
            case "contactOnly":
                if (prefs.getBoolean("contactOnly", false)) {
                    ShowToast("已设置 转发过滤");
                } else {
                    ShowToast("已取消 转发过滤");
                }
                break;
        }
    }

    private void checkNumber() {
        if (ePrefs.getString("sendToNumber", "").equals("")) {
            numPref.setSummary("请设置短信接收方的手机号");
        } else {
            if (checkStringInt(ePrefs.getString("sendToNumber", "")) && !ePrefs.getString("sendToNumber", "").equals("")) {
                numPref.setSummary("短信接收方手机号：" + ePrefs.getString("sendToNumber", ""));
            } else {
                numPref.setSummary("输入的手机号格式错误，请重新输入手机号");
                numPref.setText("");
                missedCallPref.setChecked(false);
                forwardSmsPref.setChecked(false);
            }
        }
    }

    private static boolean isMobile(String mobile) {
        Pattern p = Pattern.compile("^1[3-9][0-9]{9}$");
        Matcher m = p.matcher(mobile);
        // System.out.println(m.matches()+"---");
        return m.matches();
    }

    boolean checkStringInt(String str) {
        if (!isMobile(str)) {
            ShowToast("手机号格式错误。");
            return false;
        } else {
            return true;
        }
    }

    public void ShowToastLong(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    public void ShowToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
